package com.uenpay.im.ui.old.activity.publicnumber;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-8 上午10:48:17
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class PublicNumberAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater;
	private ArrayList<PublicNumEntity> arrayList = null;

	public PublicNumberAdapter(Context _Context,
			ArrayList<PublicNumEntity> _arrayList) {
		arrayList = _arrayList;
		layoutInflater = LayoutInflater.from(_Context);
	}

	public void setData(ArrayList<PublicNumEntity> _arrayList) {
		arrayList = _arrayList;
		notifyDataSetChanged();
	}

	public void addData(ArrayList<PublicNumEntity> _arrayList) {
		arrayList.addAll(_arrayList);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList == null ? null : arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		PublicNumEntity publicNumEntity = arrayList.get(arg0);
		PublicNumberViewHodler numberViewHodler = null;
		if (arg1 == null) {
			numberViewHodler = new PublicNumberViewHodler();
			arg1 = layoutInflater.inflate(R.layout.item_publicnum_layout, null);
			numberViewHodler.name = (TextView) arg1
					.findViewById(R.id.publicNumberNameTv);
			numberViewHodler.publicNumberIntrTv = (TextView) arg1
					.findViewById(R.id.publicNumberIntrTv);
			numberViewHodler.avatar = (RoundImageView) arg1
					.findViewById(R.id.publicNumberAvatarIv);
			arg1.setTag(numberViewHodler);
		} else {
			numberViewHodler = (PublicNumberViewHodler) arg1.getTag();
		}
		numberViewHodler.avatar.SetUrl(APIConfiguration.getAvatarUrlNormal(
				publicNumEntity.custid, PacketType.PERSONAL));
		numberViewHodler.name.setText(publicNumEntity.publicNumName);
		numberViewHodler.publicNumberIntrTv.setText(publicNumEntity.fundesc);

		return arg1;
	}

	private class PublicNumberViewHodler {
		private TextView name, publicNumberIntrTv;
		private RoundImageView avatar;
	}

}
